//
// Created by LiuYou on 2021/7/3.
//

#include <cstdlib>
#include <fcntl.h>
#include <cstdio>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include "Relayer.hpp"


#define TTY1        "/dev/tty11"
#define TTY2        "/dev/tty12"
#define TTY3        "/dev/tty9"
#define TTY4        "/dev/tty10"


int main() {
    int fd1 = open(TTY1, O_RDWR);
    if (fd1 < 0) {
        std::perror("open()");
        std::exit(1);
    }
    write(fd1, "TTY1\n", 5);
    int fd2 = open(TTY2, O_RDWR | O_NONBLOCK);
    if (fd2 < 0) {
        std::perror("open()");
        std::exit(1);
    }
    write(fd1, "TTY2\n", 5);

    int job1 = relAddJob(fd1, fd2);
    if (job1 < 0) {
        std::fprintf(stderr, "relAddJob(): %s\n", std::strerror(-job1));
        std::exit(1);
    }

    int fd3 = open(TTY3, O_RDWR);
    if (fd1 < 0) {
        std::perror("open()");
        std::exit(1);
    }
    write(fd3, "TTY3\n", 5);
    int fd4 = open(TTY4, O_RDWR | O_NONBLOCK);
    if (fd4 < 0) {
        std::perror("open()");
        std::exit(1);
    }
    write(fd4, "TTY4\n", 5);

    int job2 = relAddJob(fd3, fd4);
    if (job2 < 0) {
        std::fprintf(stderr, "relAddJob(): %s\n", std::strerror(-job2));
        std::exit(1);
    }

    while (true) {
        pause();
    }


    close(fd2);
    close(fd1);
    close(fd3);
    close(fd4);
    std::exit(0);
}